package com.edu.exam.enums.core;

import com.edu.exam.enums.core.BaseEnum;

/**
 * 四舍五入规则枚举
 */
public enum RoundingRule implements BaseEnum {
    ROUND_HALF_UP("ROUND_HALF_UP", "四舍五入"),
    ROUND_DOWN("ROUND_DOWN", "向下取整"),
    ROUND_UP("ROUND_UP", "向上取整"),
    ROUND_HALF_EVEN("ROUND_HALF_EVEN", "银行家舍入法"),
    ROUND_HALF_DOWN("ROUND_HALF_DOWN", "五舍六入"),
    FLOOR("FLOOR", "向下取整"),
    CEILING("CEILING", "向上取整"),
    NONE("NONE", "不四舍五入");

    private final String code;
    private final String description;

    RoundingRule(String code, String description) {
        this.code = code;
        this.description = description;
    }

    @Override
    public String getCode() {
        return code;
    }

    @Override
    public String getDescription() {
        return description;
    }

    /**
     * 根据编码查找枚举值
     *
     * @param code 业务编码
     * @return 对应的枚举值
     * @throws IllegalArgumentException 如果编码无效
     */
    public static RoundingRule fromCode(String code) {
        if (code == null || code.trim().isEmpty()) {
            throw new IllegalArgumentException("Rounding rule code cannot be null or empty");
        }

        for (RoundingRule rule : values()) {
            if (rule.getCode().equals(code)) {
                return rule;
            }
        }
        throw new IllegalArgumentException("Unknown RoundingRule code: " + code);
    }

    /**
     * 根据描述查找枚举值
     *
     * @param description 显示描述
     * @return 对应的枚举值
     * @throws IllegalArgumentException 如果描述无效
     */
    public static RoundingRule fromDescription(String description) {
        if (description == null || description.trim().isEmpty()) {
            throw new IllegalArgumentException("Rounding rule description cannot be null or empty");
        }

        for (RoundingRule rule : values()) {
            if (rule.getDescription().equals(description)) {
                return rule;
            }
        }
        throw new IllegalArgumentException("Unknown RoundingRule description: " + description);
    }

    @Override
    public String toString() {
        return this.code + "(" + this.description + ")";
    }
}